package cn.dnui_xqh414.dnuifood_16110100414;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.bean.AllshopBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.ShopdetailBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.getAllCommentsBean;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.rv_Adapter_foodComments;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.FoodlistModel;
import cn.dnui_xqh414.dnuifood_16110100414.model.ShopModel;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.google.android.material.button.MaterialButton;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodDetailActivity
        extends BaseActivity
        implements OnRetrofitListener<List<getAllCommentsBean>> {

    private RecyclerView recyclerView;
    private List<getAllCommentsBean> list;
    private rv_Adapter_foodComments adapter;

    private ImageView proflie;
    private TextView price,name;
    private androidx.appcompat.widget.Toolbar toolbar;
    private MaterialButton button;
    private String phoneNum;

    private ShopdetailBean bean;

    private MenuItem menuItem_comment,menuItem_contact;

    private String upload_comment = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        phoneNum = getIntent().getStringExtra("phonenum");



        int food_id = getIntent().getIntExtra("foodid",-1);
        adapter = new rv_Adapter_foodComments(this);

        FoodlistModel model = new FoodlistModel();
        model.getAllcomments(food_id,this);

        recyclerView = findViewById(R.id.rv_foodComment);

        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(manager);

        recyclerView.setAdapter(adapter);

//        String shop_id = getIntent().getStringExtra("shopid");
        model.getFoodbyId(food_id, new OnRetrofitListener<ShopdetailBean>() {
            @Override
            public void onSuccess(ShopdetailBean object, int flag) {
                bean = object;
                String url =
                        "http://172.24.10.175:8080/foodService/"+object.getPic();
                Picasso.get().load(url).placeholder(R.drawable.ic_loading)
                        .error(R.drawable.ic_load_fail_sfont).fit().centerCrop()
                        .into(proflie);

                price.setText(String.valueOf("￥"+object.getPrice()));
                name.setText(String.valueOf(object.getFoodname()));
            }

            @Override
            public void onFailure(String msg) {

            }
        });


    }

    @Override
    void initViews() {
        setContentView(R.layout.activity_food_detail);

        proflie = findViewById(R.id.img_food_head);
        price = findViewById(R.id.tv_foodDetail_foodPrice);
        name = findViewById(R.id.tv_foodDetail_foodName);
        toolbar = findViewById(R.id.toolbar_food_detail);
        button = findViewById(R.id.bt_buy);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);     //打开toolbar的返回键
        getSupportActionBar().setTitle("");
    }

    @Override
    void initEvents() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailActivity.this,PayActivity.class);
                intent.putExtra("phoneNum",phoneNum);
                intent.putExtra("foodPrice", bean.getPrice());
//                Toast.makeText(FoodDetailActivity.this, ""+bean.getPrice(), Toast.LENGTH_SHORT).show();
                intent.putExtra("foodName",bean.getFoodname());
                intent.putExtra("foodPic",bean.getPic());
                intent.putExtra("foodId",bean.getFood_id());
                startActivity(intent);
            }
        });
    }

    @Override
    void initData() {

    }
    @Override
    public void onSuccess(List<getAllCommentsBean> object, int flag) {
        adapter.setList(null);
        if (flag  == 2)
        adapter.setList(object);
    }

    @Override
    public void onFailure(String msg) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        this.menuItem_comment = menu.findItem(R.id.item_comment);
        this.menuItem_contact = menu.findItem(R.id.item_contact);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.item_comment:
                get_comment();
//                Toast.makeText(this, upload_comment, Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_contact:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phoneNum));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            default:
                Toast.makeText(this, item.toString()+" is selected.", Toast.LENGTH_SHORT).show();
                return false;
        }
        return true;
    }

    public void get_comment() {
        LayoutInflater factory = LayoutInflater.from(this);
        @SuppressLint("InflateParams") View commentView = factory.inflate(R.layout.float_edit, null);
        // removeView();
        //内部局部类，只能访问方法的final类型的变量
        final EditText comment = (EditText) commentView
                .findViewById(R.id.et_comment);
        // 不是用这个方法获取EditText的内容的
        // mname_edit.addTextChangedListener(mTextWatcher);

        // create a dialog
        new AlertDialog.Builder(FoodDetailActivity.this)
                .setIcon(R.drawable.ic_cherry_99)
                .setTitle("")
                .setView(commentView)
                .setNegativeButton("CANCEL",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // TODO Auto-generated method stub
                                dialog.cancel();
                            }
                        })
                .setPositiveButton("CONFIRM",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // dialog.
                                // TODO Auto-generated method stub
//                                if (!comment.getText().toString().equals("")) {
//                                    upload_comment = comment.getText().toString();
//                                }
                                Toast.makeText(FoodDetailActivity.this, "原来没有食物评论接口啊？？", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
    }

}
